package cz.data.auth.service;

import com.github.benmanes.caffeine.cache.CacheLoader;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.function.Function;

public class FunctionCacheLoader<K, V> implements CacheLoader<K, V> {
    private final Function<K, V> func;

    public FunctionCacheLoader(Function<K, V> func) {
        this.func = func;
    }

    @Override
    public @Nullable V load(@NonNull K key) throws Exception {
        return this.func.apply(key);
    }

    public static <K, V> FunctionCacheLoader<K, V> from(Function<K, V> func) {
        return new FunctionCacheLoader<>(func);
    }
}
